package by.amakarevich.webapp.page.address.edit;

import java.util.Date;
import java.util.List;

import javax.inject.Inject;

import org.apache.wicket.bean.validation.PropertyValidator;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.SubmitLink;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.ResourceModel;

import by.amakarevich.datamodel.Booking;
import by.amakarevich.datamodel.DeliveryAddress;
import by.amakarevich.datamodel.ShoppingCart;
import by.amakarevich.datamodel.ShoppingCartInfo;
import by.amakarevich.datamodel.UserAccount;
import by.amakarevich.services.BookingInfoService;
import by.amakarevich.services.DeliveryAddressService;
import by.amakarevich.services.ShoppingCartInfoService;
import by.amakarevich.webapp.page.AlternativeBaseLayout;
import by.amakarevich.webapp.page.booking.user.UserBookingPage;

public class AddressCreateEditPage extends AlternativeBaseLayout{
	@Inject
	private DeliveryAddressService deliveryAddressService;
	@Inject
	private ShoppingCartInfoService shoppingCartInfoService;
	@Inject
	private BookingInfoService bookingInfoService;
	
	public AddressCreateEditPage(UserAccount user, ShoppingCart shopping) {
		super();

		final ShoppingCart shoppingCart=shopping;
		final UserAccount userAccount = user;
		final Booking booking=new Booking();
		
		final DeliveryAddress deliveryAddress=new DeliveryAddress();
		
		Form<DeliveryAddress> form = new Form<DeliveryAddress>("form", new CompoundPropertyModel<DeliveryAddress>(deliveryAddress));

		final TextField<String> tfCity = new TextField<String>("city");
		tfCity.add(new PropertyValidator<String>());
		tfCity.setLabel(new ResourceModel("p.delivery.adress.city"));
		form.add(tfCity);

		final TextField<String> tfStreet = new TextField<String>("street");
		tfStreet.add(new PropertyValidator<String>());
		tfStreet.setLabel(new ResourceModel("p.delivery.adress.street"));
		form.add(tfStreet);
		
		final TextField<String> tfHouse = new TextField<String>("house");
		tfHouse.add(new PropertyValidator<String>());
		tfHouse.setLabel(new ResourceModel("p.delivery.adress.house"));
		form.add(tfHouse);
		
		final TextField<String> tfApartment = new TextField<String>("apartment");
		tfApartment.add(new PropertyValidator<String>());
		tfApartment.setLabel(new ResourceModel("p.delivery.adress.apartment"));
		form.add(tfApartment);
		
		final TextField<Long> tfPhone= new TextField<Long>("contactPhone");
		tfPhone.add(new PropertyValidator<Long>());
		tfPhone.setLabel(new ResourceModel("p.delivery.adress.phone"));
		form.add(tfPhone);
		
		final TextField<String> tfName = new TextField<String>("contactPersonName");
		tfName.add(new PropertyValidator<String>());
		tfName.setLabel(new ResourceModel("p.delivery.adress.name"));
		form.add(tfName);
		
		final TextField<String> tfLastName = new TextField<String>("contactPersonLastName");
		tfLastName.add(new PropertyValidator<String>());
		tfLastName.setLabel(new ResourceModel("p.delivery.adress.last.name"));
		form.add(tfLastName);

		form.add(new SubmitLink("sumbit-link") {
			@SuppressWarnings("deprecation")
			@Override
			public void onSubmit() {
				super.onSubmit();
				
				Date recive=new Date();
				
				Date deadline= new Date();
				
				deadline.setYear(recive.getYear());
				deadline.setMonth(recive.getMonth());
				deadline.setDate(recive.getDate()+14);
				
				booking.setReceived(recive);
				booking.setDeadlines(deadline);
				booking.setStatus(false);
				booking.setPayStatus(false);
				booking.setCanceled(false);
				booking.setTotalPrice(shoppingCart.getTotalPrice());
				
				deliveryAddressService.addNewDeliveryAddress(deliveryAddress, userAccount, booking);
				
				final List<ShoppingCartInfo> allShoppingCartInfo = shoppingCartInfoService.getAllByShoppingCart(shoppingCart);
				
				for (ShoppingCartInfo shoppingCartInfo : allShoppingCartInfo) {
					shoppingCartInfo = shoppingCartInfoService.getByShoppingCartId(shoppingCartInfo.getId());
					
					bookingInfoService.fromShoppingCartToBooking(shoppingCartInfo, userAccount, booking);
					
				}
				
				setResponsePage(new UserBookingPage(false,false));
			}

			@Override
			public void onError() {

				super.onError();
			}
		});

		add(form);

	}


}